import random
import time
import re
from ascript.android.node import Selector
from ascript.android import action
# 获取屏幕信息
from ascript.android.system import Device
display = Device.display()
# 屏幕高度
heightPixels = display.heightPixels
widthPixels = display.widthPixels
def dataTime():
    return time.time()

# 等待时间
def timeSleep(num=1):
    num = num * 1000
    time.sleep(random.randint(500, num) / 1000)

# 点击福利
def clickFuLI(num=3):
    start_time = dataTime()
    while True:
        fuli = Selector().id("com.phoenix.read:id/acv").find()
        print('点击福利')
        if fuli:
            fuli.click()
            return
        end_time = dataTime()
        if end_time-start_time>num:
            return
# 页面滑动
def slide(num=50):
    timeSleep()
    print('开始滚动元素')
    display = Device.display()
    heightPixels = display.heightPixels
    widthPixels = int(display.widthPixels / 2)
    action.slide(x=widthPixels, y=int(heightPixels / 2), x1=widthPixels,
                 y1=(int(heightPixels / 2)) - num, dur=20)
    print('滚动完成')
    timeSleep()

# 获取广告数字
def guangGaoTime():
    time_z = 2
    for itm in range(5):
        timeSleep(1)
        aa = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text(".*s后可领奖励").find()
        if aa == None:
            aa=Selector().path("/FrameLayout/WebView/WebView/View/View/View/View/View/TextView").text(".*s").find()
        if aa:
            text = aa.text
            if text != None:
                # 使用正则表达式提取所有数字
                numbers = re.findall(r'\d+', text)
                print(numbers)
                if len(numbers) > 0:
                    time_z = numbers[0]
                    print('广告等待时间', time_z)
                    time.sleep(int(time_z))
                    return
    print('广告等待时间', time_z)
    time.sleep(int(time_z))

# 点击广告
def clickGuangGao():
    print('判断是否有广告点击')
    timeSleep()
    g = 1
    is_element = Selector(1).path(
        "/FrameLayout/LinearLayout/FrameLayout/LinearLayout/FrameLayout/FrameLayout/FrameLayout/FrameLayout/LynxFlattenUI").text(
        "看视频补领一次补贴").find()
    if is_element == None:
        g = 2
        is_element = Selector().id("com.phoenix.read:id/g00").find()
    if is_element == None:
        g = 3
        is_element = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text(".*看视频再.*").find()
    if is_element == None:
        g = 4
        is_element = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("领取奖励").find()
    if is_element == None:
        g = 5
        is_element = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("看视频继续翻.*").find()
    if is_element == None:
        g = 6
        is_element = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("开心收下").find()
    if is_element == None:
        g = 7
        is_element = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("领早饭补贴.*").find()
    if is_element == None:
        g = 8
        is_element = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("领午饭补贴.*").find()
    if is_element == None:
        g = 9
        is_element = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("领晚饭补贴.*").find()
    if is_element == None:
        g = 10
        is_element = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("领夜宵补贴.*").find()
    if is_element == None:
        g = 11
        is_element = Selector().path("/FrameLayout/FrameLayout/UIText").text("看视频最高再领.*").find()
    if is_element == None:
        g = 12
        is_element = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("看视频最高再领.*").find()
    if is_element:
        print('点击广告领取奖励', g,'==')
        action.click(x=is_element.center_x, y=is_element.center_y, dur=random.randint(20, 500))
        timeSleep()
        # 点击广告后获取广告时间
        guangGaoTime()
        return
    guangGaoTime()
    return False

# 判断是否存在关闭
def closeAdvertisingOnce():
    print('校验是否关闭')
    timeSleep()
    close = Selector().id("com.byted.pangle:id/tt_reward_full_count_down_after_text").find()
    if close:
        # 有关闭两个字的需要点击关闭字眼
        action.click(x=close.center_x, y=close.center_y, dur=random.randint(20, 500))
        return True
    close = Selector().path("/FrameLayout/FrameLayout/UIView").text("明日再来").find()
    if close:
        # 有明日再来两个字的需要点击明日再来字眼
        action.click(x=close.center_x, y=close.center_y, dur=random.randint(20, 500))
        return True
    if close == None:
        close = Selector().path("/FrameLayout/LinearLayout/LinearLayout/TextView").find()
    if close == None:
        close = Selector().path("/FrameLayout/FrameLayout/LynxFlattenUI").text("领取成功").find()
    if close == None:
        close = Selector().id("com.phoenix.read:id/aee").find()
    if close == None:
        close = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("后领早饭补贴").find()
    if close == None:
        close = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("后领午饭补贴").find()
    if close == None:
        close = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("后领晚饭补贴").find()
    if close == None:
        close = Selector().path("/FrameLayout/FrameLayout/FlattenUIText").text("后领夜宵补贴").find()
    if close == None:
        close = Selector().id("com.byted.pangle:id/tt_reward_full_count_down_after_text").text("关闭").find()

    if close:
        print('关闭广告==')
        # 补充这里超时应该是一个回退
        action.slide(x=0, y=int(display.heightPixels / 2), x1=int(display.widthPixels / 2),
                     y1=int(display.heightPixels / 2), dur=random.randint(20, 100))
        timeSleep()
        # 关闭广告后判断是否有点击的广告
        clickGuangGao()
    return False